<?php
/**
 * Created by PhpStorm.
 * Script Name: BotKeyword.php
 * Create: 2023/2/13 8:12
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;
use plugin\wechat\app\model\BotKeyword as KeywordM;


class BotKeyword extends Common
{
    /**
     * 获取机器人某个关键词回复
     * @param array $params
     * @return array|mixed|\think\Collection|\think\db\Query[]
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getKeywordsByBot($params = []){
        $refresh = isset($params['refresh']) ? $params['refresh'] : false;
        unset($params['refresh']);
        $cache_key = wechat_cache_key(serialize($params));
        $data = cache($cache_key);
        if(empty($data) || $refresh){
            $bot_id = $params['bot_id'];
            $keyword = $params['keyword'];
            $data = KeywordM::where([
                ['bot_id', '=', $bot_id],
                ['keyword', '=', $keyword],
                ['status', '=', 1]
            ])->order('sort', 'desc')
                ->select();
        }
        cache($cache_key, $data);
        return $data;
    }
}